<?php

namespace app\admin\model\classroom;

use think\Db;
use think\Model;


class Course extends Model
{

    

    

    // 表名
    protected $name = 'classroom_course';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'next_status_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $image = empty($row['image']) ? '/assets/addons/classroom/img/noimage.jpg' : $row['image'];
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk],'image'=>$image]);
        });
        self::afterDelete(function ($row) {
            Db::name("classroom_course_msg")->where("course_id",$row['id'])->delete();
            Db::name("classroom_course_record")->where("course_id",$row['id'])->delete();
            Db::name("classroom_lesson")->where("course_id",$row['id'])->delete();
            Db::name("classroom_collect")->where("course_id",$row['id'])->delete();
            Db::name("classroom_test_assign")->where("course_id",$row['id'])->delete();
        });
    }

    
    public function getStatusList()
    {
        return ['1' => __('Status 1'), '0' => __('Status 0')];
    }

    public function getNextStatusList()
    {
        return ['1' => __('Next_status 1'), '0' => __('Next_status 0')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getNextStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['next_status']) ? $data['next_status'] : '');
        $list = $this->getNextStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function type()
    {
        return $this->belongsTo('Type', 'type_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    
    public function type2()
    {
        return $this->belongsTo('Type', 'type2_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function author()
    {
        return $this->belongsTo('Author', 'author_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}